package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.operations.reports.actionCommands.DataDumpReportsActionCommandFactory;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * Created by Veeru.
 * Date: Oct 18, 2005
 * Time: 5:24:44 PM
 */

public class DataDumpReportsAction extends ChipTrackExtendedAction
{
    @SuppressWarnings("unchecked")
	public ActionForward executeRequest(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HashMap<String, String> errors  = ( HashMap<String, String> ) request.getAttribute( ERROR_MESSAGES );
        HashMap<String, String> info    = ( HashMap<String, String> ) request.getAttribute( INFO_MESSAGES );
        String action   = request.getParameter( REQUEST_ACTION );
        if ( errors == null )
        {
            errors = new HashMap<String, String>();
            request.setAttribute( ERROR_MESSAGES, errors );
        }
        if ( info == null )
        {
            info = new HashMap<String, String>();
            request.setAttribute( INFO_MESSAGES, info );
        }
        if ( action == null )
        {
            action = ACTION_VIEW;
        }
        ActionCommand actionCommand = DataDumpReportsActionCommandFactory.getActionCommand( action );
        ActionForward actionForward;
        if( actionCommand == null )
        {
            Logger.error( "Could not create action instance" );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "Could not create action instance" );
            actionForward = mapping.findForward( "success" );
        }
        else
        {
            try
            {
                actionForward = actionCommand.execute( mapping, form, request, response );
            }
            catch ( Exception e )
            {
                Logger.error( "Error while executing action command", e );
                errors.put( ERROR_INTERNAL_SERVER_ERROR, "Error while executing action command" );
                pSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the action command", e ) );
                actionForward = mapping.findForward( "success" );
            }
        }
        return actionForward;
    }
}
